{
 "cells": [
  {
   "cell_type": "code",
<<<<<<< HEAD
   "execution_count": 1,
=======
   "execution_count": 6,
>>>>>>> 081c522bdcef1cb40c539a5a14ec6d26a3b53059
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "from tqdm import tqdm\n",
    "import csv\n",
    "import pickle\n",
    "import operator\n",
    "import gc\n",
    "from joblib import Parallel, delayed\n",
    "import traj_dist.distance as tdist"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
<<<<<<< HEAD
    "test_data_path = '../../data/ATest_0711.csv'\n",
=======
    "test_data_path = '../../data/testData0626.csv'\n",
>>>>>>> 081c522bdcef1cb40c539a5a14ec6d26a3b53059
    "port_info_path = '../../data/DataForModelB/port_info_dict_dump.file'\n",
    "\n",
    "carrierNameSet_info_path = '../../data/DataForModelB/data_for_train/carrierNameSet_dump.file'\n",
    "washed_train_order_brief_path = '../../data/DataForModelB/data_for_train/washed_train_order_brief.csv'\n",
    "train_data_by_order_path_folder = '../../data/DataForModelB/data_for_train/train_data_by_order'\n",
    "train_data_info_by_test_order_path_folder = '../../data/DataForModelB/data_for_train/train_data_info_by_test_order'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_data_origin = pd.read_csv(test_data_path)\n",
    "test_order_list = test_data_origin['loadingOrder'].unique()\n",
    "\n",
    "washed_train_order_brief = pd.read_csv(washed_train_order_brief_path)\n",
    "train_order_list = washed_train_order_brief['loadingOrder'].unique()"
   ]
  },
  {
   "cell_type": "code",
<<<<<<< HEAD
   "execution_count": 9,
=======
   "execution_count": 12,
>>>>>>> 081c522bdcef1cb40c539a5a14ec6d26a3b53059
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
<<<<<<< HEAD
      "  0%|          | 0/219 [02:56<?, ?it/s]\n"
=======
      "  0%|          | 0/228 [00:12<?, ?it/s]\n"
>>>>>>> 081c522bdcef1cb40c539a5a14ec6d26a3b53059
     ]
    }
   ],
   "source": [
    "def _compute_sspd_and_decide_is_useful(test_order_lon_lat, train_order):\n",
    "    train_order_data = pd.read_csv(os.path.join(train_data_by_order_path_folder, \"{}_gps_data.csv\".format(train_order)),\n",
    "                                   header=None, usecols=[1, 2], names=['longitude', 'latitude'])\n",
    "    train_order_data = np.array(train_order_data)\n",
    "    dist = tdist.sspd(train_order_data, test_order_lon_lat)\n",
    "    if dist < 1:\n",
    "        return [dist, train_order]\n",
    "    else:\n",
    "        return None\n",
    "\n",
    "def _search_related_order(test_order):\n",
    "    test_order_lon_lat = test_data_origin[test_data_origin['loadingOrder'] == test_order][['longitude', 'latitude']]\n",
    "    test_order_lon_lat = np.array(test_order_lon_lat)\n",
    "    correspond_train_order_list = Parallel(n_jobs=8)(delayed(_compute_sspd_and_decide_is_useful)\n",
    "                                (test_order_lon_lat, train_order)\n",
    "                                for train_order in train_order_list)\n",
    "    correspond_train_order_list = [item for item in correspond_train_order_list if item != None]\n",
    "    return correspond_train_order_list\n",
    "\n",
    "# 筛选出和测试集轨迹相似的训练集ID及相似度, 并存储以待后用\n",
    "for test_order in tqdm(test_order_list):\n",
    "    train_data_id_by_test_order = _search_related_order(test_order)\n",
    "    with open(os.path.join(train_data_info_by_test_order_path_folder, \"{}_related_train_data_info_dump.file\".format(test_order)), \"wb\") as f:\n",
    "        pickle.dump(train_data_id_by_test_order, f)\n",
    "    break"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
<<<<<<< HEAD
   "display_name": "Conda-python3",
   "language": "python",
   "name": "conda-python3"
=======
   "display_name": "Python 3.7.6 64-bit ('AI': conda)",
   "language": "python",
   "name": "python37664bitaiconda6859e03b37c34f0182c9bde8073269f7"
>>>>>>> 081c522bdcef1cb40c539a5a14ec6d26a3b53059
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
<<<<<<< HEAD
   "version": "3.6.4"
=======
   "version": "3.7.6"
>>>>>>> 081c522bdcef1cb40c539a5a14ec6d26a3b53059
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
